﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using log4net;
using System.Data.SqlClient;

namespace Tireguit.Nmon.UI.Elements.Logical
{
    internal class PlotElement
    {
        protected bool quietopened = false;

        protected ILog _log=LogManager.GetLogger(typeof(PlotElement));
        public IDbConnection Connection { get; protected set; }
        public PlotElement(IDbConnection connection)
        {
            Connection = connection;

        }
        protected bool QuietOpen()
        {
            try
            {
                _log.Debug("start QuietOpen");
                if (Connection.State == ConnectionState.Open)
                {
                    return true;
                }
                Connection.Open();
                quietopened = true;
                return true;
            }
            catch (System.Exception ex)
            {
                _log.Error("QuietOpen", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop QuietOpen");
            }
        }
        protected bool QuietClose()
        {
            try
            {
                _log.Debug("start QuietClose");
                if (!quietopened)
                {
                    return true;
                }
                if (Connection.State == ConnectionState.Closed)
                {
                    return true;
                }
                Connection.Close();
                quietopened = false;
                return true;
            }
            catch (System.Exception ex)
            {
                _log.Error("QuietClose", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop QuietClose");
            }

        }
        public DataTable GetCPU_ALLReader2()
        {
            IDbCommand cde = null;
            IDataReader reader = null;
            try
            {
                _log.Debug("start GetCPU_ALLReader");
                if (!QuietOpen())
                {
                    return null;
                }
                DataTable dt = new DataTable();
                cde = Connection.CreateCommand();
                cde.CommandText = "SELECT [zzzz],[Waittime] from [CPUALL_Table]";
                reader= cde.ExecuteReader();
                dt.Load(reader);
                dt.AcceptChanges();
                return dt;
            }
            catch (System.Exception ex)
            {
                _log.Error("GetCPU_ALLReader", ex);
                return null;
            }
            finally
            {
                try
                {
                    if (reader!=null)
                    {
                        reader.Dispose();
                    }
                    if (cde!=null)
                    {
                        cde.Dispose();
                    }
                    QuietClose();

                }
                catch (System.Exception)
                {

                }
                _log.Debug("stop GetCPU_ALLReader");
            }
        }
        public DataTable GetCPU_ALLReader()
        {
            IDbCommand cde = null;
            IDataReader reader = null;
            try
            {
                _log.Debug("start GetCPU_ALLReader");
                if (!QuietOpen())
                {
                    return null;
                }
                DataTable dt = new DataTable();
                cde = Connection.CreateCommand();
                cde.CommandText = "SELECT z.value as x, c.[Usertime] as y from [CPUALL_Table] c inner join [ZZZZ_Table] z on c.[zzzz] = z.[zzzz]";
                reader= cde.ExecuteReader();
                dt.Load(reader);
                dt.AcceptChanges();
                return dt;
            }
            catch (System.Exception ex)
            {
                _log.Error("GetCPU_ALLReader", ex);
                return null;
            }
            finally
            {
                try
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                    if (cde != null)
                    {
                        cde.Dispose();
                    }
                    QuietClose();
                }
                catch (System.Exception )
                {
                	
                }
                _log.Debug("stop GetCPU_ALLReader");
            }
        }
    }
}
